﻿namespace Reap.AzureResource.Web
{
	using System;
	using System.Collections.Generic;
	using System.Web.Mvc;

	using Reap.Common.Runtime;

	public class NinjectResolver : IDependencyResolver
	{
		#region Fields

		private readonly IEngine engine;

		private readonly IDependencyResolver innerResolver;

		#endregion

		#region Constructors and Destructors

		public NinjectResolver(IEngine engine, IDependencyResolver innerResolver)
		{
			this.engine = engine;
			this.innerResolver = innerResolver;
		}

		#endregion

		#region Public Methods and Operators

		public object GetService(Type serviceType)
		{
			object service = this.engine.TryResolve(serviceType);
			service = service ?? this.innerResolver.GetService(serviceType);
			return service;
		}

		public IEnumerable<object> GetServices(Type serviceType)
		{
			IEnumerable<object> services = this.engine.ResolveAll(serviceType);
			services = services ?? this.innerResolver.GetServices(serviceType);
			return services;
		}

		#endregion
	}
}